package com.sbr.seb.planning.dao.jpa.impl;

import java.util.List;

import javax.persistence.Query;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.sbr.genericdao.jpa.JPADAO;
import com.sbr.seb.entity.composants.Category;
import com.sbr.seb.entity.composants.Item;
import com.sbr.seb.entity.composants.ItemValue;
import com.sbr.seb.planning.dao.CategoryDao;

public class CategoryDaoJpaImpl extends JPADAO<Category> implements CategoryDao {

	protected static final Logger logger = LoggerFactory.getLogger(CategoryDaoJpaImpl.class);
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Category> findUpperCategory() {
		List<Category> categoryList = null;
		Query query = entityManager.createQuery("SELECT cat FROM Category cat " +
				" INNER JOIN FETCH cat.underCategoryList "); 
			
		categoryList = query.getResultList();
		
		logger.debug("findUpperCategory "+categoryList); 
		return categoryList;
		
	}
	
	@Override
	public List<Item> findItemsOfCategory(int categoryId) {
		Category category = null;
		Query query = entityManager.createQuery("SELECT cat FROM Category cat " +
				" INNER JOIN FETCH cat.items WHERE cat.id = :categoryId"); 
		
		query.setParameter("categoryId", categoryId);
		category = (Category) query.getSingleResult();
		
		logger.debug("findItemsOfCategory "+category.getItems()); 
		return category.getItems();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ItemValue> findItemValuesOfCategory(int categoryId) {
		Query query = entityManager.createQuery("SELECT val FROM ItemValue val, Item item " +
				" INNER JOIN FETCH item.itemValues WHERE item.category.id = :categoryId "); 
		
		query.setParameter("categoryId", categoryId);
		List<ItemValue> categoryList = query.getResultList();
		
		logger.debug("findItemsOfCategory "+categoryList); 
		return categoryList;
	}

}
